from lxml import etree
# 父节点1
html = etree.parse('./test.html',etree.HTMLParser())
result = html.xpath('//a[@href="link4.html"]/../@class')
print(result)
# 父节点1
result = html.xpath('//a[@href="link4.html"]/parent::*/@class')
print(result)

# 属性匹配,class属性为item-0的li节点
html = etree.parse('./test.html',etree.HTMLParser())
result = html.xpath('//li[@class="item-0"]')
print(result)
# 文本获取
html = etree.parse('./test.html',etree.HTMLParser())
result = html.xpath('//li[@class="item-0"]/text()')#li和a节点之间的换行符，，/text（）前面的/就是选取直接子节点的意思
print(result)
# 先获取子孙节点
html = etree.parse('./test.html',etree.HTMLParser())
result = html.xpath('//li[@class="item-0"]/a/text()')
print(result)
# 直接//加text（）方法
html = etree.parse('./test.html',etree.HTMLParser())
result = html.xpath('//li[@class="item-0"]//text()')
print(result)



